package com.yiren.backstage.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.yiren.backstage.currency.entity.record.ProjectInfoEntity;

import com.yiren.backstage.currency.entity.record.CarInfoRecordEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "CAR_INFO_RECORD_ENTITY".
*/
public class CarInfoRecordEntityDao extends AbstractDao<CarInfoRecordEntity, Long> {

    public static final String TABLENAME = "CAR_INFO_RECORD_ENTITY";

    /**
     * Properties of entity CarInfoRecordEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Use_line_session_id = new Property(1, String.class, "use_line_session_id", false, "USE_LINE_SESSION_ID");
        public final static Property Stu_id = new Property(2, String.class, "stu_id", false, "STU_ID");
        public final static Property Calibration = new Property(3, int.class, "calibration", false, "CALIBRATION");
        public final static Property BasePoint = new Property(4, String.class, "basePoint", false, "BASE_POINT");
        public final static Property Index = new Property(5, Long.class, "index", false, "INDEX");
        public final static Property Time = new Property(6, String.class, "time", false, "TIME");
        public final static Property IsUpload = new Property(7, boolean.class, "isUpload", false, "IS_UPLOAD");
    }

    private DaoSession daoSession;


    public CarInfoRecordEntityDao(DaoConfig config) {
        super(config);
    }
    
    public CarInfoRecordEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"CAR_INFO_RECORD_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"USE_LINE_SESSION_ID\" TEXT," + // 1: use_line_session_id
                "\"STU_ID\" TEXT," + // 2: stu_id
                "\"CALIBRATION\" INTEGER NOT NULL ," + // 3: calibration
                "\"BASE_POINT\" TEXT," + // 4: basePoint
                "\"INDEX\" INTEGER," + // 5: index
                "\"TIME\" TEXT," + // 6: time
                "\"IS_UPLOAD\" INTEGER NOT NULL );"); // 7: isUpload
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CAR_INFO_RECORD_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, CarInfoRecordEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String use_line_session_id = entity.getUse_line_session_id();
        if (use_line_session_id != null) {
            stmt.bindString(2, use_line_session_id);
        }
 
        String stu_id = entity.getStu_id();
        if (stu_id != null) {
            stmt.bindString(3, stu_id);
        }
        stmt.bindLong(4, entity.getCalibration());
 
        String basePoint = entity.getBasePoint();
        if (basePoint != null) {
            stmt.bindString(5, basePoint);
        }
 
        Long index = entity.getIndex();
        if (index != null) {
            stmt.bindLong(6, index);
        }
 
        String time = entity.getTime();
        if (time != null) {
            stmt.bindString(7, time);
        }
        stmt.bindLong(8, entity.getIsUpload() ? 1L: 0L);
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, CarInfoRecordEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String use_line_session_id = entity.getUse_line_session_id();
        if (use_line_session_id != null) {
            stmt.bindString(2, use_line_session_id);
        }
 
        String stu_id = entity.getStu_id();
        if (stu_id != null) {
            stmt.bindString(3, stu_id);
        }
        stmt.bindLong(4, entity.getCalibration());
 
        String basePoint = entity.getBasePoint();
        if (basePoint != null) {
            stmt.bindString(5, basePoint);
        }
 
        Long index = entity.getIndex();
        if (index != null) {
            stmt.bindLong(6, index);
        }
 
        String time = entity.getTime();
        if (time != null) {
            stmt.bindString(7, time);
        }
        stmt.bindLong(8, entity.getIsUpload() ? 1L: 0L);
    }

    @Override
    protected final void attachEntity(CarInfoRecordEntity entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public CarInfoRecordEntity readEntity(Cursor cursor, int offset) {
        CarInfoRecordEntity entity = new CarInfoRecordEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // use_line_session_id
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // stu_id
            cursor.getInt(offset + 3), // calibration
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // basePoint
            cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5), // index
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // time
            cursor.getShort(offset + 7) != 0 // isUpload
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, CarInfoRecordEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setUse_line_session_id(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setStu_id(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setCalibration(cursor.getInt(offset + 3));
        entity.setBasePoint(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setIndex(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5));
        entity.setTime(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setIsUpload(cursor.getShort(offset + 7) != 0);
     }
    
    @Override
    protected final Long updateKeyAfterInsert(CarInfoRecordEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(CarInfoRecordEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(CarInfoRecordEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getProjectInfoEntityDao().getAllColumns());
            builder.append(" FROM CAR_INFO_RECORD_ENTITY T");
            builder.append(" LEFT JOIN PROJECT_INFO_ENTITY T0 ON T.\"_id\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected CarInfoRecordEntity loadCurrentDeep(Cursor cursor, boolean lock) {
        CarInfoRecordEntity entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        ProjectInfoEntity projectInfo = loadCurrentOther(daoSession.getProjectInfoEntityDao(), cursor, offset);
        entity.setProjectInfo(projectInfo);

        return entity;    
    }

    public CarInfoRecordEntity loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<CarInfoRecordEntity> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<CarInfoRecordEntity> list = new ArrayList<CarInfoRecordEntity>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<CarInfoRecordEntity> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<CarInfoRecordEntity> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
